package com.flute.haflute.web;

import java.util.Properties;

import com.flute.haflute.jobbox.base.storage.RangeSelector;
import com.flute.haflute.tools.ClusterUtils;

public class QueryCountFromCassandra {
	public long getRecordCount() {
		long result = -1;
		Properties prop = new Properties();
		try {
			prop = ClusterUtils.get3rdPartConfiguration();
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		String hosts = prop.getProperty("cassandra.hosts", "localhost");
		String port = prop.getProperty("cassandra.ThriftPort", "9160");
		if (hosts.indexOf(';') == 0) {
			hosts = hosts.substring(1);
		}
		String[] hostArray = hosts.split(";");
		String[] cassandraHosts = new String[hostArray.length];
		for (int i = 0; i < hostArray.length; i++) {
			cassandraHosts[i] = hostArray[i] + ":" + port;
		}

		for (String server : cassandraHosts) {
			try {
				RangeSelector selector = new RangeSelector(server);
				selector.prepareQueryStatement("", "", -1, 1);
				result = selector.getTotalCount();
				break; // get count so return
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;
	}

	public long getRecordCount(String server) {
		long result = -1;
		try {
			RangeSelector selector = new RangeSelector(server);
			selector.prepareQueryStatement("", "", -1, 1);
			result = selector.getTotalCount();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public static void main(String[] args) {
		System.out.println(new QueryCountFromCassandra().getRecordCount());
	}
}
